package com.stellar.auth.domain.authRolePermission.impl;

import com.stellar.auth.basic.entity.AuthRolePermission;
import com.stellar.auth.basic.service.AuthRolePermissionService;
import com.stellar.auth.controller.admin.authRolePermission.vo.AuthRolePermissionRequestPageVO;
import com.stellar.auth.controller.admin.authRolePermission.vo.AuthRolePermissionRequestVO;
import com.stellar.auth.controller.admin.authRolePermission.vo.AuthRolePermissionResponseVO;
import com.stellar.auth.convert.AuthRolePermissionConvert;
import com.stellar.auth.domain.authRolePermission.AuthRolePermissionDomainService;

import com.stellar.web.core.bean.PageResult;
import com.stellar.web.core.enums.IsDeletedFlagEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色权限关联表领域服务实现
 *
 * @author Jqchan
 * @version 1.0
 * @date 2025-05-16 18:07:35
 */
@Service
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePermissionService authRolePermissionService;

    @Override
    public PageResult<AuthRolePermissionResponseVO> page(AuthRolePermissionRequestPageVO requestPageVO) {
        PageResult<AuthRolePermissionResponseVO> pageResult = new PageResult<>();
        AuthRolePermission queryEntity = AuthRolePermissionConvert.INSTANCE.convert(requestPageVO);
        queryEntity.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getVal());

        List<AuthRolePermission> entities = authRolePermissionService.page(queryEntity,
                requestPageVO.getStart(), requestPageVO.getPageSize());

        if (entities.isEmpty()) {
            return pageResult;
        }

        Long count = authRolePermissionService.count(queryEntity);
        List<AuthRolePermissionResponseVO> vos = AuthRolePermissionConvert.INSTANCE.convertEntityToRespList(entities);

        pageResult.setResult(vos);
        pageResult.setTotal(count.intValue());
        return pageResult;
    }

    @Override
    public Boolean update(AuthRolePermissionRequestVO requestVO) {
        AuthRolePermission entity = AuthRolePermissionConvert.INSTANCE.convert(requestVO);
        return authRolePermissionService.update(entity) != 0;
    }

    @Override
    public Boolean delete(Long id) {
        AuthRolePermission entity = new AuthRolePermission();
        entity.setId(id);
        entity.setIsDeleted(IsDeletedFlagEnum.DELETED.getVal());
        return authRolePermissionService.update(entity) != 0;
    }

    @Override
    public Boolean insert(AuthRolePermissionRequestVO requestVO) {
        AuthRolePermission entity = AuthRolePermissionConvert.INSTANCE.convert(requestVO);
        return authRolePermissionService.insert(entity) != 0;
    }

    @Override
    public AuthRolePermissionResponseVO get(Long id) {
        AuthRolePermission entity = authRolePermissionService.queryById(id);
        return AuthRolePermissionConvert.INSTANCE.convert(entity);
    }
}
